"""
HTML5 widgets
"""
from django import forms
from django.utils.safestring import mark_safe
from django.forms.util import flatatt
from django.utils.encoding import force_unicode

class Input(forms.Widget):
    """
    Base class for all <input> widgets (except type='checkbox' and
    type='radio', which are special).
    """
    input_type = None # Subclasses must define this.
    
    def render(self, name, value, attrs=None):
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<input%s />' % flatatt(final_attrs))

class URLInput(Input):
    input_type = 'url'
    
class EmailInput(Input):
    input_type = 'email'
    
class RegexInput(forms.TextInput):
    regex = None
    
    def render(self, name, value, attrs=None):
        attrs = attrs or {}
        if self.regex:
            attrs['pattern'] = force_unicode(self.regex)
        return super(RegexInput, self).render(name, value, attrs)

slug_re = u'^[-\w]+$'

class SlugInput(RegexInput):
    regex = slug_re

    
    